"""
Case Type   : bugfix
Case Name   : unix_timestamp时间偏移验证
Create At   : 2025/6/13
Owner       : liu-tong-8848
Description :
    1、设置默认时区查询时间
    2、设置默认+00时区查询时间偏移量
    3、设置默认+04时区查询时间偏移量
    4、设置默认+08时区查询时间偏移量
    5、设置默认-08时区查询时间偏移量
Expect      :
    1、成功
    2、成功
    3、成功
    4、成功
    5、成功
History     :
"""

import os
import unittest

from yat.test import Node
from yat.test import macro

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class BugFix(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbBmode')
        self.user_node = Node('PrimaryDbBmode')
        self.constant = Constant()

    def test_dolphin(self):
        self.log.info(f'----{os.path.basename(__file__)} start----')
        text = '----step1:设置默认时区查询时间；expect:成功----'
        self.log.info(text)
        sql_cmd = f"set timezone=default; select unix_timestamp('1970-01-01+00');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.SET_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertEqual('0', sql_res.strip().splitlines()[-2].strip(), '执行失败' + text)
        sql_cmd = f"set timezone=default; select unix_timestamp('1970-01-01+08');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.SET_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertEqual('0', sql_res.strip().splitlines()[-2].strip(), '执行失败' + text)
        sql_cmd = f"set timezone=default; select unix_timestamp('1970-01-01 00:00:00');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.SET_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertEqual('0', sql_res.strip().splitlines()[-2].strip(), '执行失败' + text)
        sql_cmd = f"set timezone=default; select unix_timestamp('1970-01-01 08:00:00');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.SET_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertEqual('0', sql_res.strip().splitlines()[-2].strip(), '执行失败' + text)
        sql_cmd = f"set timezone=default; select unix_timestamp('1970-01-01 00:00:00+00:00');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.SET_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertEqual('0', sql_res.strip().splitlines()[-2].strip(), '执行失败' + text)
        sql_cmd = f"set timezone=default; select unix_timestamp('1970-01-01 08:00:00+00:00');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.SET_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertEqual('0', sql_res.strip().splitlines()[-2].strip(), '执行失败' + text)
        sql_cmd = f"set timezone=default; select unix_timestamp('1970-01-01 00:00:00+08:00');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.SET_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertEqual('0', sql_res.strip().splitlines()[-2].strip(), '执行失败' + text)
        sql_cmd = f"set timezone=default; select unix_timestamp('1970-01-01 08:00:00+08:00');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.SET_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertEqual('0', sql_res.strip().splitlines()[-2].strip(), '执行失败' + text)


        text = '----step2:设置默认+00时区查询时间偏移量；expect:成功----'
        self.log.info(text)
        sql_cmd = f"set timezone='+00'; select unix_timestamp('1970-01-01+00');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.SET_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertEqual('0', sql_res.strip().splitlines()[-2].strip(), '执行失败' + text)
        sql_cmd = f"set timezone='+00'; select unix_timestamp('1970-01-01+08');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.SET_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertEqual('28800', sql_res.strip().splitlines()[-2].strip(), '执行失败' + text)
        sql_cmd = f"set timezone='+00'; select unix_timestamp('1970-01-01 00:00:00');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.SET_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertEqual('0', sql_res.strip().splitlines()[-2].strip(), '执行失败' + text)
        sql_cmd = f"set timezone='+00'; select unix_timestamp('1970-01-01 08:00:00');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.SET_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertEqual('28800', sql_res.strip().splitlines()[-2].strip(), '执行失败' + text)
        sql_cmd = f"set timezone='+00'; select unix_timestamp('1970-01-01 00:00:00+00:00');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.SET_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertEqual('0', sql_res.strip().splitlines()[-2].strip(), '执行失败' + text)
        sql_cmd = f"set timezone='+00'; select unix_timestamp('1970-01-01 08:00:00+00:00');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.SET_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertEqual('28800', sql_res.strip().splitlines()[-2].strip(), '执行失败' + text)
        sql_cmd = f"set timezone='+00'; select unix_timestamp('1970-01-01 00:00:00+08:00');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.SET_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertEqual('0', sql_res.strip().splitlines()[-2].strip(), '执行失败' + text)
        sql_cmd = f"set timezone='+00'; select unix_timestamp('1970-01-01 08:00:00+08:00');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.SET_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertEqual('28800', sql_res.strip().splitlines()[-2].strip(), '执行失败' + text)

        text = '----step3:设置默认+04时区查询时间偏移量；expect:成功----'
        self.log.info(text)
        sql_cmd = f"set timezone='+04'; select unix_timestamp('1970-01-01+00');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.SET_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertEqual('0', sql_res.strip().splitlines()[-2].strip(), '执行失败' + text)
        sql_cmd = f"set timezone='+04'; select unix_timestamp('1970-01-01+08');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.SET_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertEqual('14400', sql_res.strip().splitlines()[-2].strip(), '执行失败' + text)
        sql_cmd = f"set timezone='+04'; select unix_timestamp('1970-01-01 00:00:00');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.SET_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertEqual('0', sql_res.strip().splitlines()[-2].strip(), '执行失败' + text)
        sql_cmd = f"set timezone='+04'; select unix_timestamp('1970-01-01 08:00:00');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.SET_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertEqual('14400', sql_res.strip().splitlines()[-2].strip(), '执行失败' + text)
        sql_cmd = f"set timezone='+04'; select unix_timestamp('1970-01-01 00:00:00+00:00');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.SET_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertEqual('0', sql_res.strip().splitlines()[-2].strip(), '执行失败' + text)
        sql_cmd = f"set timezone='+04'; select unix_timestamp('1970-01-01 08:00:00+00:00');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.SET_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertEqual('14400', sql_res.strip().splitlines()[-2].strip(), '执行失败' + text)
        sql_cmd = f"set timezone='+04'; select unix_timestamp('1970-01-01 00:00:00+08:00');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.SET_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertEqual('0', sql_res.strip().splitlines()[-2].strip(), '执行失败' + text)
        sql_cmd = f"set timezone='+04'; select unix_timestamp('1970-01-01 08:00:00+08:00');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.SET_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertEqual('14400', sql_res.strip().splitlines()[-2].strip(), '执行失败' + text)

        text = '----step4:设置默认+08时区查询时间偏移量；expect:成功----'
        self.log.info(text)
        sql_cmd = f"set timezone='+08'; select unix_timestamp('1970-01-01+00');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.SET_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertEqual('0', sql_res.strip().splitlines()[-2].strip(), '执行失败' + text)
        sql_cmd = f"set timezone='+08'; select unix_timestamp('1970-01-01+08');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.SET_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertEqual('0', sql_res.strip().splitlines()[-2].strip(), '执行失败' + text)
        sql_cmd = f"set timezone='+08'; select unix_timestamp('1970-01-01 00:00:00');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.SET_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertEqual('0', sql_res.strip().splitlines()[-2].strip(), '执行失败' + text)
        sql_cmd = f"set timezone='+08'; select unix_timestamp('1970-01-01 08:00:00');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.SET_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertEqual('0', sql_res.strip().splitlines()[-2].strip(), '执行失败' + text)
        sql_cmd = f"set timezone='+08'; select unix_timestamp('1970-01-01 00:00:00+00:00');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.SET_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertEqual('0', sql_res.strip().splitlines()[-2].strip(), '执行失败' + text)
        sql_cmd = f"set timezone='+08'; select unix_timestamp('1970-01-01 08:00:00+00:00');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.SET_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertEqual('0', sql_res.strip().splitlines()[-2].strip(), '执行失败' + text)
        sql_cmd = f"set timezone='+08'; select unix_timestamp('1970-01-01 00:00:00+08:00');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.SET_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertEqual('0', sql_res.strip().splitlines()[-2].strip(), '执行失败' + text)
        sql_cmd = f"set timezone='+08'; select unix_timestamp('1970-01-01 08:00:00+08:00');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.SET_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertEqual('0', sql_res.strip().splitlines()[-2].strip(), '执行失败' + text)

        text = '----step5:设置默认-08时区查询时间偏移量；expect:成功----'
        self.log.info(text)
        sql_cmd = f"set timezone='-08'; select unix_timestamp('1970-01-01+00');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.SET_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertEqual('28800', sql_res.strip().splitlines()[-2].strip(), '执行失败' + text)
        sql_cmd = f"set timezone='-08'; select unix_timestamp('1970-01-01+08');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.SET_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertEqual('57600', sql_res.strip().splitlines()[-2].strip(), '执行失败' + text)
        sql_cmd = f"set timezone='-08'; select unix_timestamp('1970-01-01 00:00:00');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.SET_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertEqual('28800', sql_res.strip().splitlines()[-2].strip(), '执行失败' + text)
        sql_cmd = f"set timezone='-08'; select unix_timestamp('1970-01-01 08:00:00');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.SET_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertEqual('57600', sql_res.strip().splitlines()[-2].strip(), '执行失败' + text)
        sql_cmd = f"set timezone='-08'; select unix_timestamp('1970-01-01 00:00:00+00:00');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.SET_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertEqual('28800', sql_res.strip().splitlines()[-2].strip(), '执行失败' + text)
        sql_cmd = f"set timezone='-08'; select unix_timestamp('1970-01-01 08:00:00+00:00');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.SET_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertEqual('57600', sql_res.strip().splitlines()[-2].strip(), '执行失败' + text)
        sql_cmd = f"set timezone='-08'; select unix_timestamp('1970-01-01 00:00:00+08:00');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.SET_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertEqual('28800', sql_res.strip().splitlines()[-2].strip(), '执行失败' + text)
        sql_cmd = f"set timezone='-08'; select unix_timestamp('1970-01-01 08:00:00+08:00');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.SET_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertEqual('57600', sql_res.strip().splitlines()[-2].strip(), '执行失败' + text)

    def tearDown(self):
        self.log.info(f'----{os.path.basename(__file__)} end-----')
